package com.palandrodev.calsmart;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.palandrodev.calsmart.base.SimpleInterest;
import com.palandrodev.calsmart.util.Constants;
import com.palandrodev.calsmart.util.NumberTextWatcher;

public class SimpleInterestActivity extends Activity {
	private TextView interestViewHeading;
	private EditText principleInput;
	private EditText roiInput;
	private EditText durationInput;
	private EditText resultOutput;
	private Button calculateButton;

	public TextView getInterestViewHeading() {
		return interestViewHeading;
	}

	public void setInterestViewHeading(TextView interestViewHeading) {
		this.interestViewHeading = interestViewHeading;
	}

	public EditText getPrincipleInput() {
		return principleInput;
	}

	public void setPrincipleInput(EditText principleInput) {
		this.principleInput = principleInput;
	}

	public EditText getRoiInput() {
		return roiInput;
	}

	public void setRoiInput(EditText roiInput) {
		this.roiInput = roiInput;
	}

	public EditText getDurationInput() {
		return durationInput;
	}

	public void setDurationInput(EditText durationInput) {
		this.durationInput = durationInput;
	}

	public EditText getResultOutput() {
		return resultOutput;
	}

	public void setResultOutput(EditText resultOutput) {
		this.resultOutput = resultOutput;
	}

	public Button getCalculateButton() {
		return calculateButton;
	}

	public void setCalculateButton(Button calculateButton) {
		this.calculateButton = calculateButton;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.simple_interest_view);
		initialize();
	}

	private void initialize() {
		setInterestViewHeading((TextView) findViewById(R.id.interestViewHeading));
		setPrincipleInput((EditText) findViewById(R.id.principleInput));
		setRoiInput((EditText) findViewById(R.id.roiInput));
		setDurationInput((EditText) findViewById(R.id.durationInput));
		setResultOutput((EditText) findViewById(R.id.resultOutput));
		setCalculateButton((Button) findViewById(R.id.calculateButton));

		interestViewHeading.setText("Simple Interest");
		principleInput.setText("0.0");
		roiInput.setText("0.0");
		durationInput.setText("0.0");

		principleInput.addTextChangedListener(new NumberTextWatcher(
				principleInput));
		roiInput.addTextChangedListener(new NumberTextWatcher(roiInput));

		calculateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				float principle = 0.0f, interest = 0.0f, duration = 0.0f;
				try {
					principle = Float.valueOf(principleInput.getText()
							.toString().replaceAll(",", ""));
					interest = Float.valueOf(roiInput.getText().toString()
							.replaceAll(",", ""));
					duration = Float
							.valueOf(durationInput.getText().toString());

				} catch (Exception e) {
					Log.d(Constants.APP_NAME, e.getMessage());
				}

				SimpleInterest si = new SimpleInterest(principle, duration,
						interest);
				NumberFormat nf = new DecimalFormat("#,###.00");
				String formattedInterest = nf.format(si.calculate());
				resultOutput.setText("$ " + formattedInterest);

			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.settings, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.menu_settings:
			Intent i = new Intent(this, ShowSettingsActivity.class);
			startActivityForResult(i, Constants.RESULT_SETTINGS);
			break;

		}

		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case Constants.RESULT_SETTINGS:

			break;

		}

	}

}
